/*
 * @lc app=leetcode.cn id=1652 lang=javascript
 *
 * [1652] 拆炸弹
 */

// @lc code=start
/**
 * @param {number[]} code
 * @param {number} k
 * @return {number[]}
 */
var decrypt = function (code, k) {
  const len = code.length;
  code = code.concat(code).concat(code);
  const newCode = [];
  if (k > 0) {
    let init = 0;
    for (let i = 0; i < k; i++) {
      init += code[i];
    }
    for (let i = len; i < len + len; i++) {
      init = init - code[i] + code[i + k];
      newCode.push(init);
    }
  } else if (k === 0) {
    for (let i = len; i < len + len; i++) {
      newCode.push(0);
    }
  } else {
    let init = 0;
    for (let i = len + k; i < len; i++) {
      init += code[i];
    }
    for (let i = len; i < len + len; i++) {
      newCode.push(init);
      init = init + code[i] - code[i + k];
    }
  }
  return newCode;
};
// @lc code=end